const app = getApp();
var config = app.globalData.config;
const http = require('../../utils/http.js');

Page({
	data: {
		leavereason:"",	//请假事由
		leaveDay: "",
		studentList: [], //选择学生列表
		curriculumList: [], //选择课程列表
		addData: {
			student_index: "",
			student_id: "",
			curriculum_index: "",
			curriculum_id: ""
		}, //请假要提交的数据
	},
	onReady: function() {
		if (!app.login()) {
			return;
		}
	},
	onLoad(options) {
		this.getStudentList(); //获取选择请假学生列表
  },
  getStudentCoursearrangeList() { //获取课节
		var that = this;
		http.httpPost(config.getStudentCoursearrangeList, {
			curriculum_id:that.data.addData.curriculum_id,
			dtime:that.data.leaveDay
		}, function(e) {
			if (e.code == 1) {
        // console.log(e);
        if(e.data.length==0){
          wx.showToast({
            title: e.msg,
            icon: 'none',
            duration: 2000,
            mask: true
          });
        }
				that.setData({
					'coursearrangeList': e.data
				});
			} else {
		
			}
		
		});
  },
  checkboxChange(e) {
    // console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    // console.log(e)
    const coursearrangeList = this.data.coursearrangeList
    const values = e.detail.value
    let addData = this.data.addData;
    addData.coursearrange_id = values.toString();;
 
    for (let i = 0; i < coursearrangeList.length; i++) {
      coursearrangeList[i].checked = false

      for (let j = 0; j < values.length; j++) {
        if (coursearrangeList[i].id == values[j]) {
          coursearrangeList[i].checked = true
          break
        }
      }
    }
    this.setData({
      coursearrangeList:coursearrangeList,
      addData:addData
    })
  },
	getStudentList() {
		var that = this;
		var _content = "";
		http.httpPost(config.getLeaveStudentList, {}, function(e) {
			that.setData({
				'studentList': e.data
			});
		});
	},
	getCurriculumList(_student_id) { //获取学生报名课程
		var that = this;
		var _content = "";
		http.httpPost(config.getLeaveCurriculumList, {
			student_id: _student_id
		}, function(e) {
			var _data = e.data;
			if (_data.length == 0) {
				wx.showToast({
					title: '该学生暂无已报名课程，请先前往报名',
					icon: 'none',
					duration: 2000,
					mask: true
				})
			}
			that.setData({
				'curriculumList': e.data
      });
     
		});
	},
	//选择学生
	studentChange: function(e) {
		let addData = this.data.addData;
		addData.student_index = e.detail.value; //选中的学生列表下标
		addData.student_id = this.data.studentList[e.detail.value].id;
		this.setData({
			addData: addData
		});

		this.getCurriculumList(addData.student_id); //获取学生已报名课程列表
	},
	clickClass: function(e) {
		if (this.data.addData.student_id == "" || this.data.addData.student_id == undefined) {
			wx.showToast({
				title: '请先选择学生',
				icon: 'none',
				duration: 2000,
				mask: true
			})
    }
    
	},
	//请假课程
	curriculumChange: function(e) {
		let addData = this.data.addData;
		addData.curriculum_index = e.detail.value; //选中的学生列表下标
    addData.curriculum_id = this.data.curriculumList[e.detail.value].id;
		this.setData({
			addData: addData
		});
	},
	//请假时间
	leaveDayChange: function(e) {
		let addData = this.data.addData;
		addData.leavetime = e.detail.value;
		this.setData({
			leaveDay: e.detail.value,
			addData: addData
    });
    
     //获取课节
     this.getStudentCoursearrangeList();
	},
	getLeavereason:function(e){	//获取请假事由值
		let _leavereason = e.detail.value;
		this.setData({
			leavereason: _leavereason
		});
	},
	addStudentLeave() {
		var _leavereason = this.data.leavereason;
		var _student_id = this.data.addData.student_id;
		var _curriculum_id = this.data.addData.curriculum_id;
    var _leavetime = this.data.addData.leavetime;
    var _coursearrange_id = this.data.addData.coursearrange_id;
		
		if(_leavereason=="" || _leavereason==undefined){
			wx.showToast({
				title: '请输入【请假事由】',
				icon: 'none',
				duration: 2000,
				mask: true
			});
			return false;
		}
		if(_student_id=="" || _student_id==undefined){
			wx.showToast({
				title: '请选择【请假学生】',
				icon: 'none',
				duration: 2000,
				mask: true
			});
			return false;
		}
		if(_curriculum_id=="" || _curriculum_id==undefined){
			wx.showToast({
				title: '请选择【请假课程】',
				icon: 'none',
				duration: 2000,
				mask: true
			});
			return false;
		}
		if(_leavetime=="" || _leavetime==undefined){
			wx.showToast({
				title: '请选择【请假时间】',
				icon: 'none',
				duration: 2000,
				mask: true
			});
			return false;
		}
    if(_coursearrange_id=="" || _coursearrange_id==undefined){
			wx.showToast({
				title: '请选择【请假课节】',
				icon: 'none',
				duration: 2000,
				mask: true
			});
			return false;
		}
    
		var that = this;
		http.httpPost(config.addStudentLeaveData, {
			student_id: _student_id,
			curriculum_id:_curriculum_id,
			leavereason:_leavereason,
      leavetime:_leavetime,
      coursearrange_id:_coursearrange_id
		}, function(e) {
			var _code = e.code;
			var _msg = e.msg;
			var _icon = "none";
			var _duration = 2000;
			if(_code==1){
				_icon = "success";
			}else{
				_icon = "error";
				_duration = 3500;
			}
			
			wx.showToast({
				title: _msg,
				icon: _icon,
				duration: _duration,
				mask: true
			})
			
			if(_code==1){
				setTimeout(function(){
					wx.redirectTo({
					  url: '../AskFor_leave/AskFor_leave'
					})
				},2000);
			}
			
		});
		
	},
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})
